'use strict';

(function (app) {

    function _fomatflow(flow) {

        if (flow == 0) {
            return flow + "B/S"
        }
        var flownum = flow * 1
        var t = flownum >> 30;
        if (t > 0) {
            return (t + (flownum & ((1 << 30) - 1)) / (1 << 30)).toFixed(2) + "G/S"
        } else {
            t = flownum >> 20;
            if (t > 0) {
                return (t + (flownum & ((1 << 20) - 1)) / (1 << 20)).toFixed(2) + "M/S"
            } else {
                t = flownum >> 10
                if (t > 0) {
                    return (t + (flownum & ((1 << 10) - 1)) / (1 << 10)).toFixed(2) + "K/S"
                } else {
                    return flownum + "B/S"
                }
            }
        }
    }

    function _fomatDataSize(flow) {
        if (flow == 0) {
            return flow + "B"
        }
        var flownum = flow * 1
        var t = flownum >> 30;
        if (t > 0) {
            return (t + (flownum & ((1 << 30) - 1)) / (1 << 30)).toFixed(2) + "G"
        } else {
            t = flownum >> 20;
            if (t > 0) {
                return (t + (flownum & ((1 << 20) - 1)) / (1 << 20)).toFixed(2) + "M"
            } else {
                t = flownum >> 10
                if (t > 0) {
                    return (t + (flownum & ((1 << 10) - 1)) / (1 << 10)).toFixed(2) + "K"
                } else {
                    return flownum + "B"
                }
            }
        }
    }

//创建过滤器
    app.register.provide.value("fomatflow", _fomatflow)
//创建过滤器
    app.register.provide.value("fomatDataSize", _fomatDataSize)
    app.register.filter("fomatflow", function (fomatflow) {
        return function (text) {
            return fomatflow(text)
        }
    });
    app.register.filter("fomatDataSize", function (fomatDataSize) {
        return function (text) {
            return fomatDataSize(text)
        }
    });

    app.register.controller('monitorRealtimeCtrl', ['$scope', '$http', '$filter', '$rootScope', '$interval','$routeParams', function ($scope, $http, $filter, $rootScope, $interval,$routeParams) {

        $scope.m=!!$routeParams.m?$routeParams.m:1;
        $scope.initDraw=false;
        $scope.currentPage = 1;
        $scope.delay = 2000;
        $scope.dotcnt=20;
        $scope.adddatamodel=0;
        $scope.addpos=0;

        var recvflows = [];
        var sendflows = [];
        var recvcurpackets = [];
        var sendcurpackets = [];
        var curtimes = [];
        var onlines = [];


        function addData(datas) {
            if (!!!datas || datas.length==0) {
                return
            }
            $scope.initDraw=true;
            for (var k in datas) {
                recvflows.push(datas[k].CurReceiveDataFlow)
                sendflows.push(datas[k].CurSendDataFlow)
                var date = new Date(datas[k].CurTime);
                curtimes.push((date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds())
                onlines.push(datas[k].OnlineNum)
                recvcurpackets.push(datas[k].CurReceivePacketNum)
                sendcurpackets.push(datas[k].CurSendPacketNum)

                recvflows.shift();
                sendflows.shift();
                curtimes.shift();
                onlines.shift();
                recvcurpackets.shift();
                sendcurpackets.shift();
            }
        }
        $scope.drawInit = function (datas) {

            if (!!!datas || datas.length==0) {
                return
            }
            $scope.initDraw=true;
            for (var k in datas) {
                recvflows.push(datas[k].CurReceiveDataFlow)
                sendflows.push(datas[k].CurSendDataFlow)
                var date = new Date(datas[k].CurTime);
                curtimes.push((date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds())
                onlines.push(datas[k].OnlineNum)
                recvcurpackets.push(datas[k].CurReceivePacketNum)
                sendcurpackets.push(datas[k].CurSendPacketNum)

            }



            var option = {
                title : {
                    text: '实时网速',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['接收', '发送']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}B/S'
                        }
                    }
                ],
                series: [
                    {
                        name: '接收',
                        type: 'line',
                        data: recvflows,
                        smooth:true,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    },
                    {
                        name: '发送',
                        type: 'line',
                        data: sendflows,
                        smooth:true,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }

                ]
            };


            var sessionoption = {
                title : {
                    text: '会话数量',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['在线会话']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }

                },
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}个'
                        }
                    }
                ],
                series: [
                    {
                        name: '在线会话数量',
                        type: 'line',
                        data: onlines,
                        smooth:true,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };
            var ecoption_packets = {
                title : {
                    text: '实时报文数量',
                    subtext: ''
                },
                tooltip: {
                    trigger: 'axis'


                },
                legend: {
                    data: ['接收', '发送']
                },
                toolbox: {
                    show: true,
                    feature: {
                        mark: {show: true},
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },

                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: curtimes,
                        splitNumber: 10
                    }
                ],
                yAxis: [
                    {
                        type: 'value',
                        axisLabel: {
                            formatter: '{value}包/秒'
                        }
                    }
                ],
                series: [
                    {
                        name: '接收',
                        type: 'line',
                        data: recvcurpackets,
                        smooth:true,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    },
                    {
                        name: '发送',
                        type: 'line',
                        data: sendcurpackets,
                        smooth:true,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }

                    }
                ]
            };
            $scope.ecoption_flow = option

            $scope.ecoption_session = sessionoption;
            $scope.ecoption_packets = ecoption_packets;
        }
        $scope.drawAdd = function (datas) {
            if (!!!datas || datas.length==0) {
                return
            }
            $scope.initDraw=true;
            for (var k in datas) {
                var date = new Date(datas[k].CurTime);
                var datestr=(date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds()
                //if (curtimes[curtimes.length-1]==datestr) {
                //    return
                //}
                curtimes.push(datestr)
                recvflows.push(datas[k].CurReceiveDataFlow)
                sendflows.push(datas[k].CurSendDataFlow)



                onlines.push(datas[k].OnlineNum)
                recvcurpackets.push(datas[k].CurReceivePacketNum)
                sendcurpackets.push(datas[k].CurSendPacketNum)

                recvflows.shift();
                sendflows.shift();
                curtimes.shift();
                onlines.shift();
                recvcurpackets.shift();
                sendcurpackets.shift();
            }
            var option = {

                xAxis: [
                    {
                        data: curtimes
                    }
                ],
                series: [
                    {
                        name: '接收',
                        data: recvflows
                    },
                    {
                        name: '发送',
                        data: sendflows,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }

                ]
            };


            var sessionoption = {

                xAxis: [
                    {
                        data: curtimes
                    }
                ],
                series: [
                    {
                        name: '在线会话数量',
                        data: onlines,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    }
                ]
            };
            var ecoption_packets = {

                xAxis: [
                    {
                        data: curtimes,
                    }
                ],
                series: [
                    {
                        name: '接收',
                        data: recvcurpackets,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }
                    },
                    {
                        name: '发送',
                        data: sendcurpackets,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },
                        markLine: {
                            data: [
                                {type: 'average', name: '平均值'}
                            ]
                        }

                    }
                ]
            };
            $scope.ecoption_flow = option

            $scope.ecoption_session = sessionoption;
            $scope.ecoption_packets = ecoption_packets;
        }
        var count = 1;
        $scope.pageCount = count;

        $scope.startdate = moment().add(-1, "h").startOf("m");//moment.tz('UTC').add(-1, 'h').hour(12).startOf('h');// (start.getYear()+1900)+"-"+(start.getMonth+1)+"-"+(start.getDate()+1)+" "+start.getHours()+":"+start.getMinutes()+":"+start.getSeconds();
        $scope.enddate = moment();//moment.tz('UTC').hour(12).startOf('h');//(today.getYear()+1900)+"-"+(today.getMonth+1)+"-"+(today.getDate()+1)+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
        // $scope.sendfrom2zh=sendfrom2zh;
        var stoptime;
        $scope.init = function () {
            $rootScope.loading_text = "努力加载中...";
            console.log($scope.startdate)
            $http.post("./data/schtotal", {
                    curPage: 1,
                    count: $scope.dotcnt
                })
                .success(function (response) {
                    $scope.data = response.Datas;
                    $scope.currentPage = response.CurPage
                    $scope.totalPage = response.TotalPage
                    $scope.drawInit(response.Datas);
                    stoptime = $interval(function () {
                        $scope.sch();
                    }, $scope.delay * 1)
                });
        };
        var lastdata = null
        $scope.sch = function () {
            $rootScope.loading_text = "努力加载中...";
            $rootScope.mask = false;
            $http.post("./data/schtotal?r="+Math.random(), {
                    curPage: 1,
                    count: $scope.pageCount
                })
                .success(function (response) {
                    $scope.data = response.Datas;
                    $scope.currentPage = response.CurPage
                    $rootScope.mask = true;
                    if (!$scope.initDraw) {
                        $scope.drawInit(response.Datas)
                        return;
                    }
                    addData(response.Datas)
                    $scope.drawAdd(response.Datas);


                });
        };

        //$scope.init()

        $scope.$watch("delay", function (value) {
            $interval.cancel(stoptime)
            stoptime = $interval(function () {
                $scope.sch();
            }, $scope.delay * 1)
        })

        $scope.$watch("dotcnt", function (value) {
            $interval.cancel(stoptime)
            $scope.init()
        })
    }])

})(app)